package net.yihang;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import sun.net.www.protocol.http.HttpURLConnection;

import java.io.*;
import java.net.URL;

public class TestJsoup {

    public static void main(String[] args) throws IOException {
        String url = "https://list.jd.com/list.html?cat=1620,1621,1626";
        Document doc = Jsoup.parse(new File("C:\\Users\\lenovo\\Desktop\\test.html"),"utf-8");
//        Document doc = Jsoup.connect(url).get();
        Elements items = doc.select("li.gl-item");
        System.out.println("商品数：" + items.size());
        // 获取第一个商品的文字
        System.out.println(items.get(0).select("em").html());
        System.out.println(items.get(0).select(".p-price .J_price i"));
        // 获取第一个商品的图片地址
        String src = items.get(0).select("a img").attr("src");
        System.out.println(src);
        String filename = src.substring(src.lastIndexOf("/") + 1);
        System.out.println(filename);

        // 获取图片，并存储图片
        /*HttpURLConnection conn = (HttpURLConnection) new URL("http:" + src).openConnection();
        System.out.println(conn);
        InputStream is = conn.getInputStream();
        OutputStream os = new FileOutputStream("C:\\Users\\lenovo\\Desktop\\" + filename);
        write(is, os);*/



    }

    private static void write(InputStream is, OutputStream os) throws IOException {
        byte[] buf = new byte[1024];
        while (true) {
            int len = is.read(buf);
            if (len == -1) break;
            os.write(buf, 0, len);
        }
        is.close();
        os.close();
    }
}
